# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=distorm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.4.1
pkgrel=3
pkgdesc="Powerful disassembler library for x86/AMD64 (mingw-w64)"
arch=('any')
url="https://github.com/gdabah/distorm"
license=('GPL3')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
optdepends=("${MINGW_PACKAGE_PREFIX}-python3")
options=('strip' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/gdabah/distorm/archive/v${pkgver}.tar.gz
        Makefile
        001-detect-mingw-python.patch)
sha256sums=('ea2611b578b8efd5da780fc00e5da4043c7f5462b3a959d1dd037f0be4ae8d44'
            'a35882522a38d3ab685fd33dba746baf3704e6932e1c5707a8bd97e37ac51766'
            'eb200545cf74a51b28c8cbbc994e5d9f52fcf86b2b0600625452d4d2f4555641')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-detect-mingw-python.patch
  cp -f ${srcdir}/Makefile make/win32/

  cd ${srcdir}
  cp -rf ${_realname}-${pkgver} ${_realname}-${pkgver}-py3

  sed -e '1i#!/usr/bin/env python3' -i ${_realname}-${pkgver}-py3/examples/python/sample.py
}

build() {
  cd ${srcdir}/${_realname}-${pkgver}-py3
  make -C make/win32
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py build
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}-py3

  make -C make/win32 DESTDIR="${pkgdir}" INSTALL_PREFIX=${MINGW_PREFIX} install

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --skip-build -O1
  install -Dm 755 examples/python/sample.py -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/sample3.py"

  install -Dm 644 include/*.h -t "${pkgdir}${MINGW_PREFIX}/include"
  install -Dm 644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm 755 examples/python/sample.py -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/sample.py"
}
